import 'package:eyepetizer_example/page/video/video_detail_page.dart';
import 'package:eyepetizer_example/widget/go_navigation.dart';
import 'package:flutter/material.dart';

import '../../model/common_item.dart';
import '../../utils/cache_image.dart';
import '../../utils/date_util.dart';

class SearchItemWidget extends StatelessWidget {
  final ItemList item;

  const SearchItemWidget({Key? key, required this.item}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        if(item.data == null){
          return;
        }
        GoNavigator.toPage(context, VideoDetailPage(item.data!));
      },
      child: Stack(
        alignment: Alignment.center,
        children: <Widget>[
          // 图片
          _videoImage(),
          Positioned(
            child: Column(
              children: <Widget>[
                // 视频的 title
                _videoTitle(),
                // 视频的时长
                _videoTime(),
              ],
            ),
          ),
        ],
      ),
    );
  }

  /// 图片
  Widget _videoImage() {
    return Hero(
      tag: '${item.data?.id}${item.data?.time}',
      child: cacheImage(
        "${item.data?.cover?.feed}",
        width: double.infinity, //撑满整个屏幕
        height: 220,
      ),
    );
  }

  /// 视频的 title
  Widget _videoTitle() {
    return Text(
      "${item.data?.title}",
      maxLines: 1,
      style: const TextStyle(
          fontSize: 14, color: Colors.white, fontWeight: FontWeight.bold),
    );
  }

  /// 视频的时长
  Widget _videoTime() {
    var duration = item.data?.duration;
    String time = "";
    if(duration != null) {
      time = formatDateMsByMS(duration * 1000);
    }

    return Container(
      margin: const EdgeInsets.only(top: 10),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(5),
        child: Container(
          decoration: const BoxDecoration(color: Colors.black54),
          padding: const EdgeInsets.all(5),
          child: Text(
            time,
            style: const TextStyle(
                color: Colors.white, fontSize: 12, fontWeight: FontWeight.bold),
          ),
        ),
      ),
    );
  }
}